package com.kaishengit.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.kaishengit.dao.core.Page;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.FirstWordTag;
import com.kaishengit.pojo.Group;
import com.kaishengit.pojo.Tag;
import com.kaishengit.pojo.User;
import com.kaishengit.service.ContactService;
import com.kaishengit.service.TagService;
import com.kaishengit.service.GroupService;
import com.opensymphony.xwork2.ActionSupport;

public class ContactAction extends ActionSupport implements ServletRequestAware{
	private ContactService contactService;
	private String q_pinyin;
	private List<Contact> list;
	private HttpServletRequest request;
	private Page<Contact> contactlist;
	private Page<Contact> page = new Page<Contact>();
	private Page<Tag> paget = new Page<Tag>();
	private TagService tagService;
	private List<Tag> taglist=new ArrayList<Tag>();
	private List<FirstWordTag> fwtlist;
	private Page<Tag> tpage;//根据标签进行查询
	private String tag;//根据页面传过来的值查询联系人
	private String sousuo;
	private Contact contact;
	private String[] tel;
	private String[] teltype;
	private String[] email;
	private String[] emailtype;
	private String[] address;
	private String[] addresstype;
	private String groupview;
	private String personview;
	private String id;
	private String addtag;//从单个联系人页面传过来
	private String[] son;
	private String myall;//同时选择全部时
	private String singlecheck;//单个进行选择时
	private String[] mecheck;//获得被选的联系人的id
	private String[] telid;
	private String[] emailid;
	private String[] addressid;
	private List<Group> grouplist;
	private GroupService groupService;
	private String excelname;//导出的excelmingm
	private String outselect;//是否为全选
	private String[] selectid;//获得被选的id
	private String filename;
	private String addcontactname;
	private String addcontactcompany;
	private String addcontacttitle;
	private String addcontactmainpage;
	
	
	private static final long serialVersionUID = 1L;
	
	
	public String execute(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		System.out.println(user.getUsername());
		contactlist=contactService.findForPage(user,page);
		fwtlist=tagService.findForGroup(user);		
		return "success";
	}
	
	/**
	 * 根据标签体搜索对应的联系人
	 * @throws UnsupportedEncodingException 
	 * */

	public String byTag() throws UnsupportedEncodingException{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		//clist=contactService.findByTag(tag,user,page);
		tag=new String (tag.getBytes("iso8859-1"),"utf-8");
		System.out.println(tag);
		tpage= tagService.findTag(tag,user,paget);
		System.out.println(tpage.getResult().size());
		for(Tag tag:tpage.getResult()){
			System.out.println(tag.getContact().getTagSet().size()+"^^^^^^^^^^^^^^^^^");
			Set<Tag> t = tag.getContact().getTagSet();
			for(Tag t2:t){
				System.out.println(t2.getContent()+"00000000000000000");
			}
		}
		return "json";
	}
	
	/**
	 * 根据进行查找
	 * */
	public String bySouSuo(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		if(sousuo.equals("companyAndPerson")){
			contactlist=contactService.findForPage(user,page);
		}else{
			contactlist=contactService.findBySousuo(user,sousuo,page);
		}
		return "sousuo";
	}
	
	/**
	 * 搜索contact添加交易相关
	 * @return
	 * @throws IOException 
	 */
	public String search() throws IOException{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		list = contactService.searchContact(q_pinyin,user);
		for(Contact contact:list){
			
			contact.setAddressSet(null);
			contact.setDcSet(null);
			contact.setEmailSet(null);
			contact.setNoteSet(null);
			contact.setPhoneSet(null);
			contact.setTagSet(null);
			contact.setUser(null);
		}
		return SUCCESS;
	}
	
	
	/**
	 * 进入到单个联系人页面
	 * */
	public String tosingle(){
		contact=contactService.findById(id);
		return "success";
	}
	
	
	/**
	 * 从联系人页面保存标签
	 * */
	public String saveTag1(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		//当为全部时：
		if("all".equals(myall)){
			System.out.println(myall+"@@@@@@@@@@@@@@@@@");
			contactlist=contactService.findForPage(user,page);
			contactService.save(contactlist,user,addtag);
		}
		//当不为全部时：
		if("single".equals(singlecheck)){
			System.out.println(singlecheck+"^^^^^^^^^^^^^^^^^");
			contactService.save(mecheck,user,addtag);
		}
		return "success";
	}
	/**
	 * 从单个联系人页面保存标签
	 * */
	public String saveTag2(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		contactService.save(id,user,addtag);
		return "success";
	}
	
	/**
	 * 进入创建联系人页面
	 * */
	public String tonew(){
		grouplist=groupService.findAll();
		list=contactService.findAllPerson();
		return "success";
	}
	
	/**
	 * 保存联系人
	 * */
	public String saveContact(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		contact.setUser(user);
		contactService.save(contact,user,tel,teltype,email,emailtype,address,addresstype,groupview,personview );
		return "success";
	}
	
	/**
	 * 进入编辑联系人页面
	 * */
	public String toedit(){
		grouplist=groupService.findAll();
		contact=contactService.findById(id);
		list=contactService.findAllPerson();
		return "success";
	}
	/**
	 * 保存修改联系人信息
	 * */
	public String updateContact(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		contactService.update(user,id,contact,tel,teltype,telid,email,emailtype,emailid,address,addresstype,addressid,groupview,personview );
		return "success";
	}
	
	/**
	 * 删除联系人
	 * */
	public String delContact(){
		contactService.del(id);
		return "success";
	}
	
	/**
	 * 批量导入或导出时
	 * */
	public String addmore(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		contactlist=contactService.findForPage(user,page);
		return "success";
	}
	
	/**
	 * 导出时
	 * */
/*	public String outcontact(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		System.out.println(user.getUsername());
		contactService.outcontact(user,outselect,excelname,selectid);
		return "success";
	}*/
	//导出
		public String outcontact(){
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("curr_user");
			System.out.println(user.getUsername());
			contactlist=contactService.findForPage(user,page);
			list=contactlist.getResult();
			contactService.out(list);
			/*System.out.println("hehe");
			System.out.println(outselect);
			//System.out.println(excelname);
			for(String st:selectid){
				System.out.println(st+"%%%%%%%%%%%%%%%%%%");
			}
			//contactService.outnotall(list,excelname,selectid);
*/			return "success";
		}
	
	/**
	 * 导入时
	 * */
	public String incontact(){
		list=contactService.incontact(filename);
		for(Contact c:list){
			System.out.println(c.getName());
			System.out.println(c.getCompany());
			System.out.println(c.getTitle());
		};
		return "success";
	}
	/**
	 * 从导入的联系人中添加联系人
	 * */
	public String toAddcontact(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		contact.setUser(user);
		contactService.save(contact,user,tel,teltype,email,emailtype,address,addresstype,groupview,personview );
		return "success";
		
	}
	
	
	
	
	
	
	//set get
	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}
	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	//set  get
	public String getQ_pinyin() {
		return q_pinyin;
	}
	public void setQ_pinyin(String q_pinyin) {
		this.q_pinyin = q_pinyin;
	}
	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}
	
	public List<Contact> getList() {
		return list;
	}
	public void setList(List<Contact> list) {
		this.list = list;
	}
	
	public Page<Contact> getContactlist() {
		return contactlist;
	}
	public void setContactlist(Page<Contact> contactlist) {
		this.contactlist = contactlist;
	}
	

	public List<Tag> getTaglist() {
		return taglist;
	}
	public void setTaglist(List<Tag> taglist) {
		this.taglist = taglist;
	}


	public List<FirstWordTag> getFwtlist() {
		return fwtlist;
	}

	public void setFwtlist(List<FirstWordTag> fwtlist) {
		this.fwtlist = fwtlist;
	}
	
	public String getTag() {
		return tag;
	}
	public void setTag(String tag) {
		this.tag = tag;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setPage(Page<Contact> page) {
		this.page = page;
	}

	public String getSousuo() {
		return sousuo;
	}

	public void setSousuo(String sousuo) {
		this.sousuo = sousuo;
	}

	public Page<Tag> getTpage() {
		return tpage;
	}

	public void setTpage(Page<Tag> tpage) {
		this.tpage = tpage;
	}

	public String[] getTel() {
		return tel;
	}

	public void setTel(String[] tel) {
		this.tel = tel;
	}

	public String[] getTeltype() {
		return teltype;
	}

	public void setTeltype(String[] teltype) {
		this.teltype = teltype;
	}

	public String[] getEmail() {
		return email;
	}

	public void setEmail(String[] email) {
		this.email = email;
	}

	public String[] getEmailtype() {
		return emailtype;
	}

	public void setEmailtype(String[] emailtype) {
		this.emailtype = emailtype;
	}

	public String[] getAddress() {
		return address;
	}

	public void setAddress(String[] address) {
		this.address = address;
	}

	public String[] getAddresstype() {
		return addresstype;
	}

	public void setAddresstype(String[] addresstype) {
		this.addresstype = addresstype;
	}

	public String getGroupview() {
		return groupview;
	}

	public void setGroupview(String groupview) {
		this.groupview = groupview;
	}

	public String getPersonview() {
		return personview;
	}

	public void setPersonview(String personview) {
		this.personview = personview;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAddtag() {
		return addtag;
	}

	public void setAddtag(String addtag) {
		this.addtag = addtag;
	}

	public String[] getSon() {
		return son;
	}

	public void setSon(String[] son) {
		this.son = son;
	}

	public String getMyall() {
		return myall;
	}

	public void setMyall(String myall) {
		this.myall = myall;
	}

	public String getSinglecheck() {
		return singlecheck;
	}

	public void setSinglecheck(String singlecheck) {
		this.singlecheck = singlecheck;
	}

	public String[] getMecheck() {
		return mecheck;
	}

	public void setMecheck(String[] mecheck) {
		this.mecheck = mecheck;
	}

	public String[] getTelid() {
		return telid;
	}

	public void setTelid(String[] telid) {
		this.telid = telid;
	}

	public String[] getEmailid() {
		return emailid;
	}

	public void setEmailid(String[] emailid) {
		this.emailid = emailid;
	}

	public String[] getAddressid() {
		return addressid;
	}

	public void setAddressid(String[] addressid) {
		this.addressid = addressid;
	}

	public List<Group> getGrouplist() {
		return grouplist;
	}

	public void setGrouplist(List<Group> grouplist) {
		this.grouplist = grouplist;
	}

	public String getExcelname() {
		return excelname;
	}

	public void setExcelname(String excelname) {
		this.excelname = excelname;
	}

	public String getOutselect() {
		return outselect;
	}

	public void setOutselect(String outselect) {
		this.outselect = outselect;
	}

	public String[] getSelectid() {
		return selectid;
	}

	public void setSelectid(String[] selectid) {
		this.selectid = selectid;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getAddcontactname() {
		return addcontactname;
	}

	public void setAddcontactname(String addcontactname) {
		this.addcontactname = addcontactname;
	}

	public String getAddcontactcompany() {
		return addcontactcompany;
	}

	public void setAddcontactcompany(String addcontactcompany) {
		this.addcontactcompany = addcontactcompany;
	}

	public String getAddcontacttitle() {
		return addcontacttitle;
	}

	public void setAddcontacttitle(String addcontacttitle) {
		this.addcontacttitle = addcontacttitle;
	}

	public String getAddcontactmainpage() {
		return addcontactmainpage;
	}

	public void setAddcontactmainpage(String addcontactmainpage) {
		this.addcontactmainpage = addcontactmainpage;
	}
	

}
